package com.woniuxy.charin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.mapper.ContractsMapper;
import com.woniuxy.charin.model.entity.Contracts;
import com.woniuxy.charin.model.entity.Form;
import com.woniuxy.charin.model.request.RequestContracts;
import com.woniuxy.charin.service.ContractsService;
import com.woniuxy.charin.service.FormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/contracts")
@Api(tags = "合同管理")
public class ContractsController {
    @Autowired
    private ContractsService contractsService;
    @Autowired
    private ContractsMapper contractsMapper;
    @Autowired
    private FormService formService;
    /**
     * 发送
     */
    @PostMapping ("/send")
    @ApiOperation("发送")
    public ResponseEntity<?>send( @RequestBody Form form){
        form.setUserId(2);
        form.setStatus("待审核");
        form.setModelName("合同管理");
        formService.save(form);
        return ResponseEntity.ok(null);
    }



    /**
     * 条件查询
     */
    @PostMapping ("/query/{pageNum}/{pageSize}")
    @ApiOperation("条件查询")
    public ResponseEntity<?>query(@PathVariable Integer pageNum,@PathVariable Integer pageSize,
            @RequestBody RequestContracts requestContracts){
        LambdaQueryWrapper<Contracts> wrapper = Wrappers.lambdaQuery(Contracts.class);
        if (requestContracts.getContractName()!=null){
            wrapper.eq(Contracts::getContractName,requestContracts.getContractName());
        }
        if (requestContracts.getContractNumber()!=null){
            wrapper.eq(Contracts::getContractNumber,requestContracts.getContractNumber());
        }
        if (requestContracts.getFranchisor()!=null){
            wrapper.eq(Contracts::getFranchisor,requestContracts.getFranchisor());
        }
        if (requestContracts.getSigningDateStart()!=null&&requestContracts.getSigningDateStop()!=null){
            wrapper.between(Contracts::getSigningDate,requestContracts.getSigningDateStart(),requestContracts.getSigningDateStop());
        }
        if (requestContracts.getStatus()!=null){
            wrapper.eq(Contracts::getStatus,requestContracts.getStatus());
        }
        Page<Contracts> page = new Page<>(pageNum,pageSize);
        contractsMapper.selectPage(page,wrapper);
        return ResponseEntity.ok(page);
    }


    /**
     * 编辑
     */
    @PostMapping ("/edit")
    @ApiOperation("编辑")
    public ResponseEntity<?>edit(@RequestBody Contracts contracts){
        contracts.setStatus("待审批");
        contractsService.updateById(contracts);
        return ResponseEntity.ok(null);
    }
    /**
     * 批量删除
     */
    @PostMapping ("/batchDelete")
    @ApiOperation("批量删除")
    public ResponseEntity<?>batchDelete(@RequestBody Integer[]ids){
        List<Integer> list = Arrays.asList(ids);
        contractsMapper.deleteBatchIds(list);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据id删除
     */
    @PostMapping ("/deleteById")
    @ApiOperation("根据id删除")
    public ResponseEntity<?>deleteById(@RequestBody Contracts contracts){
        contractsService.removeById(contracts.getId());
        return ResponseEntity.ok(null);
    }


    /**
     * 根据id获取
     */
    @GetMapping("/getById")
    @ApiOperation("根据id查询")
    public ResponseEntity<?>getById(Integer id){
        Contracts byId = contractsService.getById(id);
        return ResponseEntity.ok(byId);
    }

    /**
     * 查询全部
     */
    @PostMapping("/getAll")
    @ApiOperation("显示所有合同")
    public ResponseEntity<?>getAll(@RequestBody Map<String, Integer> params){
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");

        Page<Contracts> page= contractsService.getAll(pageNum,pageSize);
        return ResponseEntity.ok(page);
    }
    /**
     * 增加合同
     */
    @ApiOperation("增加合同")
    @PostMapping("/add")
    public ResponseEntity<?>add(@RequestBody Contracts contracts){
        contracts.setStatus("待处理");
        contractsService.save(contracts);
        return ResponseEntity.ok().body(null);
    }
}
